<?php
require_once 'Zend/Db.php';
class sooonersync{
	private function db()
    	{   
        	$params = array ('host' => '127.0.0.1',
                	 'port' => '3306',
	                 'username' => 'root',
        	         'password' => 'soooner.com',
                	 'charset' => 'UTF8',
	                 'dbname'   => 'mwall');
        	$db = Zend_Db::factory('PDO_MYSQL', $params);
	        return $db;
    	}
	private function getAllDev(){
		$db = $this->db();
		$devrows = $db->fetchAll("select hosttypes.name,hostip,area1,area2 from hosts left join hosttypes on hosts.hosttypeid = hosttypes.id where hide=0");
		return $devrows;
	}
    	private function getDev($type){
	    	$db = $this->db();
		if($type!="all")
			$devrows = $db->fetchAll("select hostip from hosts left join hosttypes on hosts.hosttypeid = hosttypes.id where hide=0 and hosttypes.name = '$type'");
		else
			$devrows = $db->fetchAll("select hostip,hosttypes.tpl,ifnull(hosts.args,hosttypes.args) as args from hosts left join hosttypes on hosts.hosttypeid = hosttypes.id where hide=0");
		return $devrows;
    	}
	private function getAreas(){
		$db = $this->db();
		$areas = $db->fetchAll("select distinct hosttypes.name,area1,area2 from hosts left join hosttypes on hosts.hosttypeid = hosttypes.id");
		return $areas;
	}
	private function getTreeid($name,$parentnode){
		if($parentnode)
			$treeid = system('(cat nodemap.list|grep '.$name.'|awk \'{if($3=='.$parentnode.') print $2}\')');
		else
			$treeid = system('(cat nodemap.list|grep '.$name.'|awk \'{print $2}\')');
		return $treeid;
	}
    	private function getTemplateId($tname){
     		$tpl = system('(php ../add_device.php --list-host-templates|grep \''.$tname.'\'|awk \'{print $1}\')');
		return $tpl;
    	}
	private function getSnmpqueryid($sname){
		$snmpqueryid = system('(php ../add_graphs.php --list-snmp-queries|grep \''.$sname.'\'|awk \'{print $1}\')');
		return $snmpqueryid;
	}
	private function getSnmpquerytypeid($name,$snmpqueryid){
		$snmpquerytypeid = system('(php ../add_graphs.php --list-query-types  --snmp-query-id='.$snmpqueryid.'|grep \''.$name.'\'|awk \'{print $1}\')');
		return $snmpquerytypeid;
	}
	private function getGraphtemplateid($name){
		$gtplid = system('(php ../add_graphs.php  --list-graph-templates|grep \''.$name.'\'|awk \'{print $1}\')');
		return $gtplid;
	}
	private function addDev($hostip,$tpl){
    		system("php ../add_device.php --description=$hostip --ip=$hostip --template=$tpl --version=2 --community=soooner --avail=snmp"); 
    	}
	private function addPortGragh($hostid,$graphtemplateid,$snmpqueryid,$snmpquerytypeid,$port){
		echo "php ../add_graphs.php --host-id=$hostid --graph-type=ds --graph-template-id=$graphtemplateid --snmp-query-id=$snmpqueryid --snmp-query-type-id=$snmpquerytypeid --snmp-field=ServiceIndex --snmp-value=$port";
		system("php ../add_graphs.php --host-id=$hostid --graph-type=ds --graph-template-id=$graphtemplateid --snmp-query-id=$snmpqueryid --snmp-query-type-id=$snmpquerytypeid --snmp-field=ServiceIndex --snmp-value=$port");
	}
	private function addHDGragh($hostid,$graphtemplateid,$snmpqueryid,$snmpquerytypeid){
		system("php ../add_graphs.php --host-id=$hostid --graph-type=ds --graph-template-id=$graphtemplateid --snmp-query-id=$snmpqueryid --snmp-query-type-id=$snmpquerytypeid --snmp-field=hrStorageAllocationUnits --snmp-value='4096'");
	}
	private function addIFGragh($hostid,$graphtemplateid,$snmpqueryid,$snmpquerytypeid){
		system("php ../add_graphs.php --host-id=$hostid --graph-type=ds --graph-template-id=$graphtemplateid --snmp-query-id=$snmpqueryid --snmp-query-type-id=$snmpquerytypeid --snmp-field=ifSpeed --snmp-value=1000000000");
	}
	private function addCGraph($hostid,$gragh_tpl_id){
		system("php ../add_graphs.php --host-id=$hostid --graph-type=cg --graph-template-id=$gragh_tpl_id");
	}
	private function addTreeNode($name,$treeid,$parentnode){
		if($parentnode){
			system("php ../add_tree.php --type=node --name=$name --node-type=header --tree-id=$treeid --parent-node=$parentnode");
		}
		else{
			system("php ../add_tree.php --type=node --name=$name --node-type=header --tree-id=$treeid");
		}
	}
	private function getHostMap(){
		system('php ../add_graphs.php --list-hosts>hostmap.list');
	} 
	private function getNodeMap(){
		system('php ../add_tree.php --list-nodes --node-type=header --tree-id=2>nodemap.list');
	}
	private function getHostId($ip){
		$id=system('(cat hostmap.list|grep '.$ip.'|awk \'{print $1}\')');
		return $id;
	}
	private function getTplMap(){
		$hosttplstr = system('(php ../add_device.php --list-host-templates|grep soooner|awk \'{printf"%s:%s ",$1,$2}\')');
		$hosttpls = explode(" ",$hosttplstr);
		$result = array();
		foreach($hosttpls as $hosttpl){
			$p = explode(":",$hosttpl);
			$detailstr = system('(php ../add_graphs.php --list-graph-templates --host-template-id=11 |awk \'{printf "%s ",$1}\')');
			$detailstr = str_replace('Known ','',$detailstr);
			$details = explode(" ",$detailstr);
			$result[$p[1]]["cg"]=$details;
			$result[$p[1]]["id"]=$p[0];
		}
		return $result;
	}
	public function str2array($str = "test:1,2,3;test2:1"){
		$result = array();
		$arr1 = explode(";",$str);
		foreach($arr1 as $item){
			$arr2 = explode(":",$item);
			$arr3 = explode(",",$arr2[1]);
			$result[$arr2[0]] = $arr3;
		}
		print_r($result);
		return $result;
	}
	public function syncAllDevs(){
		$devrows = $this->getDev("all");
                foreach($devrows as $row){
                        $tpl = $this->getTemplateId($row["tpl"]);
                        $hostip = $row["hostip"] ;
                        $this->addDev($hostip,$tpl);
                }
	}
	public function HostsAddCGraph(){
		$devrows = $this->getDev("all");
		$this->getHostMap();
		$tplMap=$this->getTplMap();
		foreach($devrows as $row){
			$gragh_tpl_ids = $tplMap[$row["tpl"]]["cg"];
			$hostid = $this->getHostId($row["hostip"]);	
			foreach($gragh_tpl_ids as $gragh_tpl_id){
				$this->addCGraph($hostid,$gragh_tpl_id);
			}
		}
	}
	public function HostAddHD(){
		$graphtemplateid = $this->getGraphtemplateid("Host MIB - Available Disk Space");
                $snmpqueryid = $this->getSnmpqueryid("SNMP - Get Mounted Partitions");
                $snmpquerytypeid = $this->getSnmpquerytypeid("Available Disk Space",$snmpqueryid);
		$this->getHostMap();	
		$devrows = $this->getDev("all");
		foreach($devrows as $row){
			$hostid = $this->getHostId($row["hostip"]);
                        $this->addHDGragh($hostid,$graphtemplateid,$snmpqueryid,$snmpquerytypeid);
		}
	}
	public function HostAddIf(){
		$graphtemplateid = $this->getGraphtemplateid("Interface - Traffic (bits/sec)");
                $snmpqueryid = $this->getSnmpqueryid("SNMP - Interface Statistics");
                $snmpquerytypeid = $this->getSnmpquerytypeid("In/Out Bits (64-bit Counters)",$snmpqueryid);
                $this->getHostMap();
                $devrows = $this->getDev("all");
                foreach($devrows as $row){
                        $hostid = $this->getHostId($row["hostip"]);
                        $this->addIFGragh($hostid,$graphtemplateid,$snmpqueryid,$snmpquerytypeid);
                }
	}
	public function HostAddPort(){
		$graphtemplateid = $this->getGraphtemplateid("TCP");
                $snmpqueryid = $this->getSnmpqueryid("TCP");
                $snmpquerytypeid = $this->getSnmpquerytypeid("TCP",$snmpqueryid);
                $this->getHostMap();
                $devrows = $this->getDev("all");
                foreach($devrows as $row){
                        $hostid = $this->getHostId($row["hostip"]);
			$ports = $this->str2array($row["args"]);
			foreach($ports as $port){
				if($port[0])
                        		$this->addPortGragh($hostid,$graphtemplateid,$snmpqueryid,$snmpquerytypeid,$port[0]);
			}
                }
	}
	public function InitTrees(){
		$this->getNodeMap();
		$areas = $this->getAreas();
		foreach($areas as $row){
			$n1=$row["name"];
			$n2=$row["area1"];
			$n3=$row["area2"];
			$treeid = $this->getTreeid($n1,null);
			if(!$treeid){
				$this->addTreeNode($n1,2);
				$treeid = $this->getTreeid($n1,null);
			}
			$treeid = $this->getTreeid($n1,null);
			if($n2&&$treeid){
				$treeid_1 = $this->getTreeid($n2,$treeid);
                        	if(!$treeid_1){
					$this->addTreeNode($n2,2,$treeid);
					$treeid_1=$this->getTreeid($n1,$treeid);
				}
			}
			$treeid_1 = $this->getTreeid($n2,$treeid);
			if($n3&&$treeid_1){
                                $treeid_2 = $this->getTreeid($n3,$treeid_1);
                                if(!$treeid_2){
                                        $this->addTreeNode($n3,2,$treeid_1);
                                }
                        }
		}
	}	
	public function hostToTree(){
		$this->getHostMap();
		$devrows = $this->getAllDev();
		foreach($devrows as $row){
			$n1 = $row["name"];
			$n2 = $row["area1"];
			$n3 = $row["area2"];
			$hostid = $this->getHostId($row["hostip"]);
			$pnode = 0;
			$id_1 = 0;
			$id_2 = 0;
			$id = $this->getTreeid($n1,null);
			if($n2&&$id){
				$id_1 = $this->getTreeid($n2,$id);
			}
			if($n3&&$id_1){
				$id_2 = $this->getTreeid($n3,$id_1);
			}
			if($id_2)$pnode=$id_2;
			elseif($id_1)$pnode=$id_1;
			elseif($id){
				$pnode=$id;
				echo "####################";
				echo "php ../add_tree.php --type=node --node-type=host --tree-id=2 --parent-node=$pnode --host-id=$hostid";
			}
			system("php ../add_tree.php --type=node --node-type=host --tree-id=2 --parent-node=$pnode --host-id=$hostid ");
		}
	}
}
$sooonersync = new sooonersync();
$sooonersync->syncAllDevs();
$sooonersync->InitTrees();
$sooonersync->InitTrees();
$sooonersync->InitTrees();
$sooonersync->InitTrees();
$sooonersync->hostToTree();
$sooonersync->HostsAddCGraph();
$sooonersync->HostAddHD();
$sooonersync->HostAddIF();
$sooonersync->HostAddPort();
